var websocket = require('websocket-driver'),
	restify= require('restify'),app= restify.createServer(),path= require('path');
app.use(restify.queryParser())
app.use(restify.CORS())
app.use(restify.fullResponse())
app.use(restify.gzipResponse())
app.use(restify.acceptParser(app.acceptable))
app.get('/', function (req, res, next){
  var data = require('fs').readFileSync(path.join(__dirname, 'index.html'));
  res.status(200);
  res.header('Content-Type', 'text/html');
  res.end(data.toString());
})
app.get(/^.*\.(html|png|svg|css|js|ico)$/,restify.serveStatic({directory: path.join(__dirname)}));

app.on('upgrade', function(request, socket, body) {
  if (!websocket.isWebSocket(request)) return;

  var driver = websocket.http(request);

  driver.io.write(body);
  socket.pipe(driver.io).pipe(socket);

  driver.messages.on('data', function(message) {
    console.log('Got a message', message);
	driver.text('你好')
  });

  driver.start();
})

app.listen(80)